package bmatch.tech;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

/**
 * Hello world!
 */
public class App {

  public static void main(String[] args) throws Exception {

    List<String> warnings = new ArrayList<String>();
    boolean overwrite = true;
    File configFile = new File("generatorConfig.xml");

    File file = new File("code");
    if (file.exists()){
      file.delete();
    }
    file.mkdir();
    Properties properties = getProperties();

    ConfigurationParser cp = new ConfigurationParser(properties,warnings);
    Configuration config = cp.parseConfiguration(configFile);
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    myBatisGenerator.generate(null);
    if (!warnings.isEmpty()) {
      for (String warn : warnings) {
        System.out.println(warn);
      }
    }
    System.out.println("生成成功！");
  }

  /**
   * 自定义配置
   * @return
   */
  private static Properties getProperties() {
    Properties properties = new Properties();
    properties.setProperty("url","jdbc:mysql://116.85.0.241:27931/bmatchdb");
    properties.setProperty("userId","bmatch");
    properties.setProperty("password","Bmatch123");
    // 包名
    properties.setProperty("package","bmatch.tech.hello");
    // 要生成的表名
    properties.setProperty("tableName","common_city_code");
    return properties;
  }
}
